package com.baosight.sczx.zm.ck.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.Engineering;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.cb.domain.ZMCBRoadway;
import com.baosight.sczx.zm.cg.domain.ZMCGAccident;
import com.baosight.sczx.zm.ck.domain.ZMCKJqap;

/**
 * 掘进日上报
 * <p>Title: ServiceZMCP95.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-9-6 上午06:30:28  严洪浩  Create
 *</pre>
 */
public class ServiceZMCK40 extends ServiceEPBase {
	
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP95</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-6上午06:31:00 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP95</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-6上午06:31:28 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {	
		StaticTools.setQueryParameters(inInfo, "fdMiningId", UserSession.getInSessionProperty("miningCode"), true);		
		EiInfo outInfo = super.query(inInfo, "ZMCKJqap.query", new ZMCKJqap());
		outInfo.addBlock(getMines()); 
		outInfo.addBlock(StaticBlocks.getStauts4());
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,UserSession.getInSessionProperty("miningCode").toString()));
		outInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));//矿ID的默认值
		return outInfo;
	}
	
	public EiBlock getMines() {
		EiBlock block = new EiBlock("cmes.ch.mine");

		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);

		List<Map> _listData = new ArrayList<Map>();
		try {
			_listData = dao.query("ZMCKJxAp.queryTreeChild", null);
		} catch (Exception e) {}
		
		for(int i = 0; i < _listData.size(); i++) {
			_list.add(_listData.get(i)); 
		} 
		block.setRows(_list);
		return block;
	}

	
	/**
	 * 新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP95</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-6上午09:05:43 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 		
		String date = StaticTools.getDate();
		String fdStatus = "草拟";
		String fdReportPerson = (String) UserSession.getInSessionProperty("UserCname");
		String fdReportPersonId = (String) UserSession.getUserId();
		ZMCKJqap _dom = new ZMCKJqap();
		String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");//矿ID的默认值
		String fdMiningName = (String) UserSession.getInSessionProperty("miningName");//矿名称的默认值
//		String fdReportDepartmentId = (String) UserSession.getInSessionProperty("orgCode") == null ? "" : UserSession.getInSessionProperty("orgCode").toString();//发送部门
		String fdReportDepartmentId =  UserSession.getInSessionProperty("orgCode").toString();
		String fdReportDepartment =  UserSession.getInSessionProperty("orgName").toString();
		
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdStatus(fdStatus);
			_dom.setFdCurrentDate(date);
			_dom.setFdReportPerson(fdReportPerson);
			_dom.setFdReporterId(fdReportPersonId);
			_dom.setFdReportDepartmentId(fdReportDepartmentId);
			_dom.setFdReportDepartment(fdReportDepartment);
			_dom.setFdMiningId(fdMiningId);
			_dom.setFdMiningName(fdMiningName);
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CK_JQAP"));
			this.dao.insert("ZMCKJqap.insert",_dom); 
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  
		}
		
	
	/**
	 * 修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP95</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-6上午09:09:01 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out=new EiInfo();
		String date = StaticTools.getDate();
		String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");//矿ID的默认值
		String fdMiningName = (String) UserSession.getInSessionProperty("miningName");//矿名称的默认值
		String fdReportPerson = (String) UserSession.getInSessionProperty("UserCname");
		String fdReportDepartmentId = (String) UserSession.getInSessionProperty("orgCode") == null ? "" : UserSession.getInSessionProperty("orgCode").toString();//发送部门
		String fdReportPersonId = (String) UserSession.getUserId();
		
		ZMCKJqap _dom = new ZMCKJqap();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			if(validate(_dom)){
			_dom.setFdCurrentDate(date);
			_dom.setFdReportPerson(fdReportPerson);
			_dom.setFdReporterId(fdReportPersonId);
			_dom.setFdReportDepartmentId(fdReportDepartmentId);
			_dom.setFdMiningId(fdMiningId);
			_dom.setFdMiningName(fdMiningName);
			this.dao.update("ZMCKJqap.update",_dom);
			}else{
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("只能修改草拟状态的记录!"); 
				return out;
			}
			
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  

		}
	

	
	/**
	 * 删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午03:58:35 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCKJqap _dom = new ZMCKJqap();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			if(validate(_dom)){
				this.dao.delete("ZMCKJqap.delete", _dom);
				}else{
					out = this.query(inInfo);
					out.setStatus(-1);
					out.setMsg("只能删除草拟状态的记录!"); 
					return out;
				}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   

	}
	
	/**
	 * 上报
	 * <p>Function: modifyStop</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-13下午08:12:20 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public EiInfo report(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCKJqap _dom = new ZMCKJqap();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			if(validate(_dom)){
			this.dao.update("ZMCKJqap.report",_dom);
			}else{
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("只能上报草拟状态的记录!"); 
				return out;
			}
		}
		EiInfo outInfo = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}	


	
//	public EiInfo generate(EiInfo inInfo) {
//		EiInfo out=new EiInfo(); 
//		ZMCKJqap _dom = new ZMCKJqap();
//		EiBlock eiBlock = inInfo.getBlock("result");
//		eiBlock.setBlockMeta(_dom.eiMetadata);
//		int rowcount = eiBlock.getRowCount();
//		int i = 0;
//		for (; i < rowcount; i++) {
//			_dom.fromMap(eiBlock.getRow(i));
//			if(validate(_dom)){
//			this.dao.update("ZMCKJqap.report",_dom);
//			}else{
//				out = this.query(inInfo);
//				out.setStatus(-1);
//				out.setMsg("只能上报草拟状态的记录!"); 
//				return out;
//			}
//		}
//		EiInfo outInfo = this.query(inInfo);
//		out.setStatus(1);
//		out.setMsg("操作" + rowcount + "记录成功");
//		return outInfo;
//	}	
//	
//	
//	/**
//	 * 将发布的数据复制到拷贝表中
//	 * <p>Function: copy</p>
//	 * <p>Description: </p>
//	 * <p>return value:void</p>
//	 * <p>throws Exception:</p>
//	 * <p>History:
//	 *    2011-9-9下午03:25:53 张志芳
//	 * </p>
//	 *
//	 */
//	private void copy(ZMCPGrubReport _dom)
//	{
//		Map<String, String> params = new HashMap<String, String>();
//		params.put("fdDate", _dom.getFdDate());
//		params.put("fdWorknumId", _dom.getFdWorknumId());
//		params.put("fdReportDepartmentId", _dom.getFdReportDepartmentId());
//		List<?> result = dao.query("ZMCPGrubReportCopy.validate", params);
//		if(result != null && result.size() > 0) {			
//		}
//		else
//		{
//			_dom.setFdStatus((long)0);			
//		}			
//		dao.insert("ZMCPGrubReportCopy.insert",_dom);
//	}
	
	private boolean validate(ZMCKJqap _dom){
		  String fdStatus = _dom.getFdStatus();
		  if("草拟".equals(fdStatus)){
			 return true; 
		  }else
			  return false;
		  
	}	
	
	
	
}


			

